/**
 *
 * @author sio
 */
package control;

import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.TreeSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.Pays;
import modele.Connect;
import modele.DAO;
import modele.Med;
import modele.Dep;

/**
 *
 * @author sio
 */
public class Control extends HttpServlet {

    private Pays pays1;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        pays1 = new Pays(); //instancie les objets utiles
    }

    public void destroy() {
        Connect.close();
    }

    protected void processRequest(
            HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        String choix = request.getParameter("choix");
        String page;
        if (choix == null) {
            page = "index.jsp";

        } else {
            if ("dep".equals(choix)) {
                String choixdep = request.getParameter("choixdep");
                if (choixdep == null) {
                    Collection<Dep> lesDeps = pays1.getLesDeps();
                    request.setAttribute("lesDeps", lesDeps);
                    page = "dep.jsp";
                } else {
                    request.setAttribute("dep", choixdep);
                    request.setAttribute("lesMeds", pays1.getLeDep(choixdep).getLesMeds());
                    page = "listemed.jsp";
                }
            } else {
                if ("spe".equals(choix)) {
                    String choixspe = request.getParameter("choixspe");
                    page = "spe.jsp";
                } else {
                    String choixnom = request.getParameter("choixnom");
                    if (choixnom == null) {
                        page = "nom.jsp";
                    } else {
                        Collection<Med> lesMeds = new TreeSet<Med>();
                        for (Dep d : pays1.getLesDeps()) {
                            for (Med m : d.getLesMeds()) {
                                if (m.getNom().startsWith(choixnom)) {
                                    lesMeds.add(m);
                                }
                            }
                        }
                        request.setAttribute("lesMeds", lesMeds);
                        page = "listemed.jsp";
                    }
                } 
            }
        }
        //appel de la page JSP
        request.getRequestDispatcher(page).forward(request, response);
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
